package com.jinguanjia.stock.web.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.fasterxml.jackson.databind.util.JSONPObject;


@RestController
public class IndexController {

	private static final Logger logger = LoggerFactory.getLogger(IndexController.class);

	@Autowired
    RestTemplate template;
	
	@Autowired
	private DiscoveryClient discoversy;

	private Object object;
	
	@RequestMapping("/consumer")
    public String index() {
        return template.getForEntity("http://EUREKA-PROVIDER/hello", String.class).getBody();
    }
	
	
	@RequestMapping(value = "/discovery", method = RequestMethod.GET)
	public String discovery()
	{	
		
		List<String> list = discoversy.getServices();  //获取注册中心的所有服务 
		List<ServiceInstance> instances = discoversy.getInstances("EUREKA-PROVIDER"); //获取名为EUREKA-PROVIDER服务
		StringBuilder result = new StringBuilder();
		for (ServiceInstance element : instances) {
			result.append(element.getServiceId() + "\t" + element.getHost() + "\t" + element.getPort() + "\t"
					+ element.getUri() );
		}
		return result.toString();
	}
	
}
